if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v_tb_saledtlvirtual]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[v_tb_saledtlvirtual]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE   view v_tb_saledtlvirtual
as
  select p.producttrademarkid, d.*,isnull(d.cprice,0) as m_acprice from tb_saledtl d 
    left join tb_sale m on m.bid=d.bid  
    left join td_product p on d.productid=p.p_id
    --left join td_producttrademark t on p.producttrademarkid=t.id
    where m.bkind=1 or m.bkind=2 or m.bkind =5
  union 
  select p.producttrademarkid, d.*,isnull(tt.price,0) as m_acprice from tb_saledtl d 
    left join tb_sale m on m.bid =d.bid 
    left join td_product p on d.productid=p.p_id    
    left join tt_billvirtual tt on d.bid=tt.bid and d.bno=tt.bno and m.bkind=tt.bkind and tt.btype=240
    where m.bkind=3 or m.bkind=4


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

